import type { FC } from 'react'
import React from 'react'
import { useTranslation } from 'react-i18next'
import RemoveEffectVarConfirm from '../_base/components/remove-effect-var-confirm'
import useConfig from './use-config'
import type { SQLOperatorNodeType } from './types'
import VarList from '@/components/workflow/nodes/_base/components/variable/var-list'
import OutputVarList from '@/components/workflow/nodes/_base/components/variable/output-var-list'
import AddButton from '@/components/base/button/add-button'
import Field from '@/components/workflow/nodes/_base/components/field'
import Split from '@/components/workflow/nodes/_base/components/split'
import CodeEditor, { CodeLanguage } from '@/components/workflow/nodes/_base/components/editor/code-editor'
import TypeSelector from '@/components/workflow/nodes/_base/components/selector'
import { type NodePanelProps } from '@/components/workflow/types'
import BeforeRunForm from '@/components/workflow/nodes/_base/components/before-run-form'
import ResultPanel from '@/components/workflow/run/result-panel'
import { Switch } from 'antd'
import VarReferencePicker from '../_base/components/variable/var-reference-picker'
import { VarType } from '../tool/types'

const i18nPrefix = 'workflow.nodes.sql'

const datasources = [
  {
    label: 'Mysql',
    value: 'Mysql',
  },
]
const Panel: FC<NodePanelProps<SQLOperatorNodeType>> = ({
  id,
  data,
}) => {
  const { t } = useTranslation()

  const {
    readOnly,
    inputs,
    outputKeyOrders,
    handleScriptAndVarsChange,
    handleVarListChange,
    handleAddVariable,
    handleRemoveVariable,
    handleScriptChange,
    handleIsQueryChange,
    handleDataSourceVarChange,
    handleVarsChange,
    handleAddOutputVariable,
    filterVar,
    isShowRemoveVarConfirm,
    hideRemoveVarConfirm,
    onRemoveVarConfirm,
    // single run
    isShowSingleRun,
    hideSingleRun,
    runningStatus,
    handleRun,
    handleStop,
    runResult,
    varInputs,
    inputVarValues,
    setInputVarValues,
  } = useConfig(id, data)

  return (
    <div className='mt-2'>
      <div className='px-4 pb-4 space-y-4'>
        <Field
          title={t(`${i18nPrefix}.inputVars`)}
          operations={
            !readOnly ? <AddButton onClick={handleAddVariable} /> : undefined
          }
        >
          <VarList
            readonly={readOnly}
            nodeId={id}
            list={inputs.variables}
            onChange={handleVarListChange}
            filterVar={filterVar}
            isSupportFileVar={false}
          />
        </Field>
        <Field title={t(`${i18nPrefix}.isQuery`)}  >
          <Switch
            defaultValue={!!inputs.is_query}
            value={!!inputs.is_query}
            onChange={handleIsQueryChange}
            disabled={readOnly}
          />
        </Field>
        <Field title={t(`${i18nPrefix}.dataSource`)}  >
          <VarReferencePicker
            nodeId={id}
            readonly={readOnly}
            isShowNodeName
            className='grow'
            value={inputs.datasource || []}
            isSupportConstantValue={false}
            onChange={handleDataSourceVarChange}
            defaultVarKindType={VarType.variable}
            onlyLeafNodeVar={false}
            filterVar={filterVar}
            isSupportFileVar={false}
          />
        </Field>
        <Split />
        <CodeEditor
          isInNode
          readOnly={readOnly}
          title={<>SQL语句</>}
          language={CodeLanguage.sql}
          value={inputs.script}
          onChange={handleScriptChange}
          showCodeGenerator={true}
        />
      </div>
      <Split />
      <div className='px-4 pt-4 pb-2'>
        <Field
          title={t(`${i18nPrefix}.outputVars`)}
          operations={
            <AddButton onClick={handleAddOutputVariable} />
          }
        >

          <OutputVarList
            readonly={readOnly}
            outputs={inputs.outputs}
            outputKeyOrders={outputKeyOrders}
            onChange={handleVarsChange}
            onRemove={handleRemoveVariable}
          />
        </Field>
      </div>
      {
        isShowSingleRun && (
          <BeforeRunForm
            nodeName={inputs.title}
            onHide={hideSingleRun}
            forms={[
              {
                inputs: varInputs,
                values: inputVarValues,
                onChange: setInputVarValues,
              },
            ]}
            runningStatus={runningStatus}
            onRun={handleRun}
            onStop={handleStop}
            result={<ResultPanel {...runResult} showSteps={false} />}
          />
        )
      }
      <RemoveEffectVarConfirm
        isShow={isShowRemoveVarConfirm}
        onCancel={hideRemoveVarConfirm}
        onConfirm={onRemoveVarConfirm}
      />
    </div >
  )
}

export default React.memo(Panel)
